package org.ucas.kgraph.configuration;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.ucas.kgraph.utils.security.ShiroDbRealm;

@Configuration
//@ImportResource({"classpath:plugins/shiro.xml"})
public class ShiroConfiguration {

  private static Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();

  @Bean(name = "shiroDbRealm")
  public ShiroDbRealm getShiroRealm() {
    return new ShiroDbRealm();
  }

  @Bean(name = "shiroEhcacheManager")
  public EhCacheManager getEhCacheManager() {
    EhCacheManager em = new EhCacheManager();
    em.setCacheManagerConfigFile("classpath:plugins/ehcache-shiro.xml");
    return em;
  }

  @Bean(name = "lifecycleBeanPostProcessor")
  public LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
  }

  @Bean
  public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
    DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
    daap.setProxyTargetClass(true);
    return daap;
  }

  @Bean(name = "securityManager")
  public DefaultWebSecurityManager getDefaultWebSecurityManager() {
    DefaultWebSecurityManager dwsm = new DefaultWebSecurityManager();
    dwsm.setRealm(getShiroRealm());
    dwsm.setCacheManager(getEhCacheManager());
    return dwsm;
  }

  @Bean
  public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor() {
    AuthorizationAttributeSourceAdvisor aasa = new AuthorizationAttributeSourceAdvisor();
    aasa.setSecurityManager(getDefaultWebSecurityManager());
    return new AuthorizationAttributeSourceAdvisor();
  }

  @Bean(name = "shiroFilter")
  public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(getDefaultWebSecurityManager());
    shiroFilterFactoryBean.setLoginUrl("/login.html");
    //filterChainDefinitionMap.put("/user/**", "authc,perms[user]");
    //filterChainDefinitionMap.put("/**", "authc,perms[admin]");
    //文件夹管理//重命名、删除     文件夹
    filterChainDefinitionMap.put("/files/**/rename", "authc,perms[files：rename]");
    filterChainDefinitionMap.put("/files/**/delete", "authc,perms[files：delete]");
    filterChainDefinitionMap.put("/files", "authc,perms[files：create]");
    
    
    //我的案例
    filterChainDefinitionMap.put("/admin/selfplib.html", "authc,perms[admin：selfplib]");
    	//我的案例中搜索创建
    filterChainDefinitionMap.put("/terms/roots/search", "authc");
    filterChainDefinitionMap.put("/terms/search", "authc");
    filterChainDefinitionMap.put("/admin/selfpptreat.html", "authc,perms[admin：selfpptreat]");
    filterChainDefinitionMap.put("/admin/selfpmtreat.html", "authc,perms[admin：selfpptreat]");
    filterChainDefinitionMap.put("/terms/search/draw", "authc,perms[admin：selfpptreat]");
    filterChainDefinitionMap.put("/schems/search/draw", "authc,perms[admin：selfpptreat]");
    filterChainDefinitionMap.put("/schems/search", "authc,perms[admin：selfpptreat]");
    filterChainDefinitionMap.put("/schems/roots/search", "authc,perms[admin：selfpptreat]");
    	//精确搜索创建
    filterChainDefinitionMap.put("/admin/selfpmctreat.html", "authc,perms[admin：selfpmctreat]");
    //filterChainDefinitionMap.put("/terms/searchrelate/draw", "authc,perms[admin：selfpmctreat]");
    	//重命名(可以分为三个)、提交、删除   导图
    filterChainDefinitionMap.put("/schems/**/reroot", "authc,perms[schems：reroot]");
    filterChainDefinitionMap.put("/schems/**/submitp", "authc,perms[schems：submitp]");
    filterChainDefinitionMap.put("/schems/**/deletesp", "authc,perms[schems：deletesp]");
    
    filterChainDefinitionMap.put("/admin/selfptreat.html", "authc,perms[schems：editmapsp]");
    //filterChainDefinitionMap.put("/schems/get", "authc,perms[schems：editmapsp]");
    //filterChainDefinitionMap.put("/schems/roots/select", "authc,perms[schems：editmapsp]");
    //filterChainDefinitionMap.put("/schems/**/recontent", "authc,perms[schems：editmapsp]");
    
    //我的素材
    filterChainDefinitionMap.put("/admin/selfmlib.html", "authc,perms[admin：selfmlib]");
    	//提交、删除   导图
    filterChainDefinitionMap.put("/terms/**/submitm", "authc,perms[terms：submitm]");
    filterChainDefinitionMap.put("/schems/**/deletesm", "authc,perms[schems：deletesm]");
    	//新建导图
    filterChainDefinitionMap.put("/schems", "authc,perms[schems：create]");
    
    filterChainDefinitionMap.put("/admin/selfmtreat.html", "authc,perms[schems：editmapsm]");
    
    
    //素材库
    filterChainDefinitionMap.put("/admin/mlib.html", "authc,perms[admin：mlib]");
    filterChainDefinitionMap.put("/terms/roots", "authc,perms[admin：mlib]");
    filterChainDefinitionMap.put("/terms", "authc,perms[terms：create]");
    	//重命名、移动
    filterChainDefinitionMap.put("/terms/**/rename", "authc,perms[terms：rename]");
    filterChainDefinitionMap.put("/terms/**/refile", "authc,perms[terms：refile]");
    filterChainDefinitionMap.put("/terms/**/deleteroot", "authc,perms[terms：deleteroot]");
    	//编辑素材
    filterChainDefinitionMap.put("/terms/**/delete", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/cut", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/retags", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/deleteDocument", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/retheme", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/redirection", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/recost", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/newdocument", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/**/links", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/admin/mtreat.html", "authc,perms[terms：editmap]");
    filterChainDefinitionMap.put("/terms/roots/select", "authc,perms[terms：editmap]");
    
    //案例库
    filterChainDefinitionMap.put("/admin/plib.html", "authc,perms[admin：plib]");
    filterChainDefinitionMap.put("/schems/**/movep", "authc,perms[schems：movep]");
    filterChainDefinitionMap.put("/schems/**/deletep", "authc,perms[schems：deletep]");
    filterChainDefinitionMap.put("/admin/ptreat.html", "authc,perms[schems：editmapp]");
    filterChainDefinitionMap.put("/admin/selfmctreat.html", "authc,perms[admin：selfmctreat]");
    filterChainDefinitionMap.put("/schems/pcreate", "authc,perms[schems：pcreate]");
    //filterChainDefinitionMap.put("/terms/searchrelate/draw", "authc,perms[admin：selfmctreat]");
    
    //用户管理
    filterChainDefinitionMap.put("/admin/useradmin.html", "authc,perms[admin：useradmin]");
    filterChainDefinitionMap.put("/users/list", "authc,perms[admin：useradmin]");
    filterChainDefinitionMap.put("/users/**/editRole", "authc,perms[users：editRole]");
    filterChainDefinitionMap.put("/users/**/editDisable", "authc,perms[users：editDisable]");
    filterChainDefinitionMap.put("/users/**/delete", "authc,perms[users：delete]");
    
    
    //角色管理
    filterChainDefinitionMap.put("/admin/roleadmin.html", "authc,perms[admin：roleadmin]");
    
    filterChainDefinitionMap.put("/roles", "authc,perms[roles：create]");
    filterChainDefinitionMap.put("/roles/edit", "authc,perms[roles：edit]");
    filterChainDefinitionMap.put("/roles/**/delete", "authc,perms[roles：delete]");
    
    
    
    //filterChainDefinitionMap.put("/admin/selfadmin.html", "authc,perms[admin：selfadmin]");
    //filterChainDefinitionMap.put("/terms", "authc,perms[terms：get]");
    //filterChainDefinitionMap.put("/terms/theme", "authc,perms[terms：theme]");
    //filterChainDefinitionMap.put("/terms/**/parent", "authc,perms[terms：parent]");
    //filterChainDefinitionMap.put("/terms/**/children", "authc,perms[terms：children]");
    //schems权限
    //filterChainDefinitionMap.put("/schems/roots", "authc,perms[schems：roots]");
    //filterChainDefinitionMap.put("/schems/get", "authc,perms[schems：get]");
    //filterChainDefinitionMap.put("/schems/roots/select", "authc,perms[schems：roots_select]");
    //filterChainDefinitionMap.put("/schems/**/recontent", "authc,perms[schems：recontent]");
    //个人管理
    filterChainDefinitionMap.put("/admin/selfadmin.html", "authc");
    
    filterChainDefinitionMap.put("/**", "anon");//anon
    shiroFilterFactoryBean
            .setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
  }

}
